package com.zhugang.week13.program;

/**
 * @program algorithms
 * @description: defangIpaddr
 * @author: chanzhugang
 * @create: 2022/09/11 12:00
 */
public class DefangIpaddr2 {

    /**
     * 1108 IP地址无效化
     * https://leetcode.cn/problems/defanging-an-ip-address/
     *
     * @param address
     * @return
     */
    public String defangIpaddr(String address) {
        // 开辟一个新数组，拷贝原数组数据到新数组
        int n = address.length();
        char[] newArr = new char[n + 6];
        int k = 0;
        for (int i = 0; i < n; i++) {
            char c = address.charAt(i);
            if (c != '.') {
                newArr[k++] = c;
            } else {
                newArr[k++] = '[';
                newArr[k++] = c;
                newArr[k++] = ']';
            }
        }
        return new String(newArr);
    }
}